using System.IO;
using System.Windows.Forms;
using HogoLogic;

namespace ProjectView
{
	public partial class RepositoryEditorForm : Form
	{
		#region Singletone
		private RepositoryEditorForm()
		{
			InitializeComponent();
		}
		
		public static readonly RepositoryEditorForm Instance = new RepositoryEditorForm();
		#endregion


		public HogoProject SelectedProject
		{
			get
			{
				return new HogoProject(nameTextBox.Text,
					workingDirTextBox.Text);
			}
			private set
			{
				nameTextBox.Text = value.Name;
				workingDirTextBox.Text = value.WorkingDir;
			}
		}

		public DialogResult ShowDialog(HogoProject project)
		{
			SelectedProject = project;
			return ShowDialog();
		}

		private void selectFolderButton_Click(object sender, System.EventArgs e)
		{
			if (folderBrowserDialog.ShowDialog() == DialogResult.OK)
			{
				workingDirTextBox.Text = folderBrowserDialog.SelectedPath;
				if (nameTextBox.Text.Length == 0)
					nameTextBox.Text = folderBrowserDialog.SelectedPath;
			}
		}

		private void RepositoryEditorForm_FormClosing(object sender, FormClosingEventArgs e)
		{
			if (DialogResult == DialogResult.OK)
			{
				if (nameTextBox.Text.Length == 0)
				{
					MessageBox.Show("Enter correct project name", "Project name is empty", MessageBoxButtons.OK, MessageBoxIcon.Error);
					e.Cancel = true;
				}

				if (!Directory.Exists(workingDirTextBox.Text))
				{
					MessageBox.Show("Enter correct working directory", "Working dir isn't exist", MessageBoxButtons.OK, MessageBoxIcon.Error);
					e.Cancel = true;
				}
			}
		}
	}
}